/*
 * @(#) $Header$
 *
 * Copyright (C)  2010  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package ca.forklabs.baselib.util.predicate;

import ca.forklabs.baselib.util.BinaryPredicate;
import ca.forklabs.baselib.util.UnaryPredicate;

/**
 * A {@code Bind2ndPredicate} is a predicate adapter that transforms a
 * {@link BinaryPredicate} into an {@link UnaryPredicate} by binding the second
 * argument of the binary function.
 *
 * @param   <A1>   the type of the first argument object.
 * @param   <A2>   the type of the second argument object.
 *
 * @author <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.predicate.Bind2ndPredicate">Daniel Léonard</a>
 * @version $Revision$
 */
public class Bind2ndPredicate<A1, A2> implements UnaryPredicate<A1> {

//---------------------------
// Instance variable
//---------------------------

   /** The adapted binary predicate. */
   private BinaryPredicate<A1, A2> the_predicate;

   /** The binded value. */
   private A2 the_arg2;


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    * @param   predicate   the binary predicate to adapt.
    * @param   arg2   the binded argument.
    * @exception   NullPointerException   if the predicate parameter is
    *                                     {@code null}.
    */
   public Bind2ndPredicate(BinaryPredicate<A1, A2> predicate, A2 arg2) {
      this.setPredicate(predicate);
      this.setArg2(arg2);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Changes the binary predicate.
    * @param   predicate   the new binary predicate.
    * @exception   NullPointerException   if the predicate is {@code null}.
    */
   @SuppressWarnings("nls")
   protected void setPredicate(BinaryPredicate<A1, A2> predicate) {
      if (null == predicate) {
         String message = this.getNullFunctionErrorMessage("f(x, c)");
         throw new NullPointerException(message);
         }
      this.the_predicate = predicate;
      }

   /**
    * Returns the adapted binary function.
    * @return   the adapted binary function.
    */
   protected BinaryPredicate<A1, A2> getPredicate() {
      return this.the_predicate;
      }

   /**
    * Changes the binded argument.
    * @param   arg2   the new binded argument.
    */
   protected void setArg2(A2 arg2) {
      this.the_arg2 = arg2;
      }

   /**
    * Returns the binded argument.
    * @return   the binded argument.
    */
   protected A2 getArg2() {
      return this.the_arg2;
      }


//---------------------------
// Implemented method from ca.forklabs.baselib.util.UnaryPredicate
//---------------------------

   /**
    * Invokes the adapted predicate with the binded argument as the second
    * argument and the parameter as the second argument.
    * @param  arg1   the first argument.
    * @return   the results returned by the adapted predicate.
    */
   @Override
   public boolean eval(A1 arg1) {
      BinaryPredicate<A1, A2> predicate = this.getPredicate();
      A2 arg2 = this.getArg2();

      boolean evaluation = predicate.eval(arg1, arg2);
      return evaluation;
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the error message saying that a function is {@code null}.
    * @param   function   the null function.
    * @return   the error message.
    */
   protected String getNullFunctionErrorMessage(String function) {
      String key = Resources.NULL_PREDICATE;
      String message = Resources.getLocalizedString(key, function);
      return message;
      }

   }
